package com.knight.concurrent.chapter12;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

/**
 * @Description:
 * @author: knight
 * @Date: 2019-03-17 19:57
 */
public class JDKCountDown {


    public static Random random = new Random(System.currentTimeMillis());

    private static CountDownLatch countDownLatch = new CountDownLatch(5);
    public static void main(String[] args) throws InterruptedException {
        IntStream.rangeClosed(1, 5).forEach(i ->
                new Thread(() -> {
                    System.out.println(i + " is working");
                    try {
                        Thread.sleep(random.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    countDownLatch.countDown();
                }, "Thread" + i).start()
        );

        countDownLatch.await();

        System.out.println("多线程任务全部运行结束");
        System.out.println("finish");
    }

}
